package com.gzdh.device.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.device.comm.vo.Result;
import com.gzdh.device.config.annotation.IgnoreAuthorize;
import com.gzdh.device.device.entity.MachineDevice;
import com.gzdh.device.device.service.MachineDeviceService;
import com.gzdh.device.shop.entity.ShopMachine;
import com.gzdh.device.shop.service.ShopMachineService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.security.PermitAll;
import java.util.Date;




@RestController
@RequestMapping("api/cMachine/init")
public class DeviceInitController {

    @Autowired
    ShopMachineService shopMachineService;

    @Autowired
    MachineDeviceService machineDeviceService;

    @GetMapping("findDevice")
    @IgnoreAuthorize
    public Result<Object> findDevice(@RequestParam("deviceSn") String deviceSn) {
        Result<Object> result = new Result<>();
        ShopMachine shopMachine = null;
        MachineDevice machineDevice = machineDeviceService.getOne(new QueryWrapper<MachineDevice>().lambda().eq(MachineDevice::getDeviceSn, deviceSn));
        if (machineDevice == null) {
            //设备为空，需要添加
            machineDevice = new MachineDevice();
            machineDevice.setDeviceSn(deviceSn);
            machineDevice.setCreateTime(new Date());
//            machineDevice.setUpTenantMqtt(false);
            machineDeviceService.save(machineDevice);
        }
//        checkDeviceSn(deviceSn);
        if (StringUtils.isNotEmpty(machineDevice.getShopId())) {
            shopMachine = shopMachineService.getOne(new QueryWrapper<ShopMachine>().lambda().eq(ShopMachine::getShopId, machineDevice.getShopId()));
        }
        result.setResult(shopMachine);
        return result;
    }

}
